'use strict';

angular.module('app')
    .controller('updateController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "";
            $scope.imgData={};

            if($state.includes('**.platInfo.update')){
                title="编辑资讯平台信息";
                var platNo = $state.params.id;
                activate(platNo);
                selectPlatInfo(null);
                $scope.isDisabled = false;
            }else if($state.includes('**.platInfo.add')){
                title="添加资讯平台";
                selectPlatInfo();
                setTimeout(function(){
                    !$rootScope.$$phase && $scope.$apply();
                },300);
            }
            $scope.title = $rootScope.title = title;
            $scope.loading = true;

            //初始化验证
            $scope.submit= function(){
                $scope.loading = true;
                var platName = $("#platName").val();
                var platDesc = $("#platDesc").val();
                if(!$('#platName').val()){
                    $scope.alertModal({'content':'资讯平台名称不能为空'});
                    return;
                }else {
                    if(platName.length>10){
                        $scope.alertModal({"content":"平台名称不能超过10个字"});
                        return;
                    }
                }
                if(!$('#platDesc').val()){
                    $scope.alertModal({'content':'平台简介不能为空'});
                    return;
                }else {
                    if(platDesc.length>150){
                        $scope.alertModal({"content":"内容不能超过150个字"});
                        return;
                    }
                }

                if(!$('#moduleNo').val()){
                    $scope.alertModal({'content':'平台所属分类不能为空'});
                    return;
                }
                saveData();
            };

            function saveData(){
                var detailUrlLogo = $scope.imgData[0];
                if (!detailUrlLogo) {
                    $scope.alertModal({'content': '请上传Logo图'});
                    return;
                }
                var picUrl = $scope.imgData[0].src;
                $scope.record.platLogo = picUrl[0];

                var m = $scope.record;
                console.log(m);
                if(m){
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
                        url : $scope.record.platNo ? $scope.app.project_name+'/platInfoManage/platInfo/update' : $scope.app.project_name+'/platInfoManage/platInfo/add',
                        data: m
                    }).then(callback);
                }
                function callback(result){
                    if(result.httpCode ==200){
                        $scope.alertModal({"content":"操作成功"});
                        $timeout(function(){
                            $state.go('main.plat.platInfo.list');
                        },2000);
                    }else{
                        $scope.alertModal({"content":"操作失败"});
                        $scope.isDisabled = false;
                    }
                    $scope.loading = false;
                }
            }

            // 初始化页面
            function activate(platNo) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name + '/platInfoManage/platInfo/detail',
                    data: {'platNo': platNo}
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                        $scope.imgData = [];
                        var srcArr = [];
                        srcArr.push($scope.record.platLogo);
                        $scope.imgData.push({
                            src:srcArr
                        })
                        console.log($scope.imgData)
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            // 资讯平台所属分类对应的moduleName
            function selectPlatInfo() {
                $scope.loading = true;
                $scope.selected = '';
                $.ajax({
                    url : $scope.app.project_name+ '/platInfoManage/platInfo/selPlatInfoNameList',
                    data: {}
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.platInfoList = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

        }]);